package com.lyy.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;

import com.github.pagehelper.PageInfo;
import com.lyy.entity.Order;
import com.lyy.Vo.OrderVo;
import com.lyy.Vo.OrderVoPage;
import com.lyy.entity.param.OrderInsertParam;
import com.lyy.entity.param.OrderUpdateParam;
import com.lyy.result.Result;
import com.lyy.service.OrderService;
import lombok.Data;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Liangyaoyun
 */
@Data
@ResponseBody
@RequestMapping("/api/v1/order")
public class OrderController {
    public OrderService orderService;

    @RequestMapping("/select-by-order-id/{order-id}")
    public Result selectByOrderId(@PathVariable("order-id") Integer orderId) {
        Order order = orderService.selectByOrderId(orderId);
        if (ObjectUtil.isEmpty(order)) {
            return Result.failure();
        }
        OrderVo orderVo = new OrderVo();
        BeanUtil.copyProperties(order, orderVo);
        return Result.success(orderVo);
    }

    @RequestMapping("/insert")
    public Result insert(@RequestBody OrderInsertParam orderInsertParam) {
        Order order = BeanUtil.copyProperties(orderInsertParam, Order.class);
        int result = orderService.insertByOrderId(order);
        return result > 0 ? Result.success() : Result.failure();
    }

    @RequestMapping("/update")
    public Result update(@RequestBody OrderUpdateParam orderUpdateParam) {
        Order order = BeanUtil.copyProperties(orderUpdateParam, Order.class);
        int result = orderService.updateByOrderId(order);
        return result > 0 ? Result.success() : Result.failure();
    }

    @RequestMapping("/list")
    public Result list() {
        List<Order> orders = orderService.selectAllOrder();
        if (ObjectUtil.isEmpty(orders)) {
            return Result.failure();
        }
        List<OrderVo> orderVos = BeanUtil.copyToList(orders, OrderVo.class);
        return Result.success(orderVos);
    }

    @RequestMapping("/delete-by-order-id/{order-id}")
    public Result deleteByOrderId(@PathVariable("order-id") Integer orderId) {
        int result = orderService.deleteByOrderId(orderId);
        return result > 0 ? Result.success() : Result.failure();
    }

    @RequestMapping("/delete-by-order-ids")
    public Result deleteByBannerIds(@RequestParam("order-ids") Integer[] orderIds) {
        int result = orderService.deleteByOrderIds(List.of(orderIds));
        return result > 0 ? Result.success() : Result.failure();
    }

    @RequestMapping("/page")
    public Result page(@RequestParam("page") Integer page,
                       @RequestParam("size") Integer size) {
        PageInfo<Order> result = orderService.selectBypage(page, size);
        if (ObjectUtil.isEmpty(result)) {
            return Result.failure();
        }
        OrderVoPage orderVoPage = new OrderVoPage();
        orderVoPage.setPage(result.getPageNum());
        orderVoPage.setSize(result.getPageSize());
        orderVoPage.setTotal(result.getTotal());
        orderVoPage.setPages(result.getPages());
        orderVoPage.setOrderVos(BeanUtil.copyToList(result.getList(),OrderVo.class));
        return Result.success(orderVoPage);
    }
    @RequestMapping("/today-count")
    public Result todayCount() {
        return Result.success(orderService.todayCount());
    }
    @RequestMapping("/total-count")
    public Result totalCount() {
        return Result.success(orderService.totalCount());
    }
    @RequestMapping("/growth-count")
    public Result growthCount() {
        return Result.success(orderService.growthCount());
    }
}
